﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using AjaxControlToolkit;

using System.Web.Script.Serialization;
using System.Diagnostics;
using System.ComponentModel;

[assembly: System.Web.UI.WebResource("Kevin.Controls.LeftMenu.LeftMenu.js", "text/javascript")]

namespace Kevin.Controls
{

    [RequiredScript(typeof(CommonToolkitScripts))]
    [RequiredScript(typeof(AnimationExtender))]
    [ClientScriptResource("Kevin.Controls.LeftMenu", "Kevin.Controls.LeftMenu.LeftMenu.js")]    
    public class LeftMenu:ScriptControlBase
    {
        public LeftMenu():base(true,HtmlTextWriterTag.Div)
        { 

        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Page.RegisterRequiresControlState(this);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            //CreateLeftMenu();
            //Page.ClientScript.GetCallbackEventReference(this, "", "", "");
        }

        private void CreateLeftMenu()
        {
            if (this.MenuList == null)
            {
                throw new Exception("未指定MenuList,如有疑问请联系我（席吉波）");
            }
            string strMenu = @"<li><a href='{0}' title={1} id='menu{5}' target='{6}'
                            class={2} {4}>{3}</a></li>";
            string strMenuNonePower = @"<li><a class={0}  title={1} {2}>{3}</a></li>";
            System.Web.UI.WebControls.Label lblMenu = new System.Web.UI.WebControls.Label();
            //List<MenuItem> list = new JavaScriptSerializer().Deserialize<List<MenuItem>>(this.MenuList);
            
            string[] powerArray=this.PowerList.Split(',');
            foreach (MenuItem item in MenuList)
            {
                if (powerArray.Contains(item.PowerID))
                    lblMenu.Text += string.Format(strMenu,
                        ResolveUrl(item.Link),
                        item.Text,
                        item.CssClass,
                        item.Text,
                        "",
                        item.ID,
                        this.Target);
                //else
                //    lblMenu.Text += string.Format(strMenuNonePower, 
                //        item.CssClass,
                //        "您无权限操作",
                //        "style='FILTER:Alpha(opacity=30);-moz-opacity:0.01;opacity:0.01; cussor:hand;' onclick='alert(\"您无权限操作\");'",
                //        item.Text);
            }
            
            this.Controls.Add(lblMenu);
        }

        private MenuType MenuType
        {
            get { return MenuType.Left; }
        }
        

        public string PowerList
        {
            get { return ViewState["PowerList"].ToString(); }
            set { ViewState["PowerList"] = value; }
        }
        //List<MenuItem> _MenuList;
        
        public List<MenuItem> MenuList
        {
            get { return (List<MenuItem>)ViewState["_MenuList"]; }
            set { ViewState["_MenuList"] = value; }
        }

        private string ClientList
        {
            get;
            set;
        }
        
        private string _CurrentLink = "";
        public string CurrentLink
        {
            set { _CurrentLink = value; }
            get { return _CurrentLink; }
        }

        [ExtenderControlProperty]
        [ClientPropertyName("cssClassName")]
        public string CssClassName
        {
            get { return GetPropertyValue<string>("cssClassName", "leftMenu"); }
            set { SetPropertyValue<string>("cssClassName", value); }
        }

        //当MenuStyle为Popup时，需指定此属性，请选择框架集所在页面的body
        [ExtenderControlProperty]
        [ClientPropertyName("popupMenuParent")]
        public string PopupMenusParent
        {
            get { return GetPropertyValue<string>("popupMenuParent", "document.body"); }
            set { SetPropertyValue<string>("popupMenuParent",value); }
        }

        //菜单项连接的目标框架
        [ExtenderControlProperty]
        [ClientPropertyName("target")]
        public string Target
        {
            get { return GetPropertyValue<string>("target","document.body"); }
            set { SetPropertyValue<string>("target",value); }
        }


        [ExtenderControlProperty]
        [ClientPropertyName("iconPosetion")]
        public IconPosetionEnum IconPosetion
        {
            get { return GetPropertyValue<IconPosetionEnum>("iconPosetion", IconPosetionEnum.Left); }
            set { SetPropertyValue<IconPosetionEnum>("iconPosetion", value); }
        }

        [DefaultValue(MenuStyle.Popup)]
        [ExtenderControlProperty]
        [ClientPropertyName("menuStyle")]
        public MenuStyle MenuStyle
        {
            get { return GetPropertyValue<MenuStyle>("menuStyle", MenuStyle.Popup); }
            set { SetPropertyValue<MenuStyle>("menuStyle", value); }
        }

        /// <summary>
        /// 默认需展开的菜单项ID
        /// </summary>
        [ExtenderControlProperty]
        [ClientPropertyName("defaultExpandMenuId")]
        public string DefaultExpandMenuId
        {
            get { return GetPropertyValue<string>("defaultExpandMenuId", ""); }
            set { SetPropertyValue<string>("defaultExpandMenuId", value); }
        }

        /// <summary>
        /// 默认需展开的第一级菜单序列号，默认为0即第一项
        /// </summary>
        [ExtenderControlProperty]
        [ClientPropertyName("defaultExpandMenuIndex")]
        public int DefaultExpandMenuIndex
        {
            get { return GetPropertyValue<int>("defaultExpandMenuIndex", 0); }
            set { SetPropertyValue<int>("defaultExpandMenuIndex", value); }
        }

        protected override void LoadClientState(string clientState)
        {
            MenuClientState MenuClientState = new JavaScriptSerializer().Deserialize<MenuClientState>(clientState);
            this.MenuList = MenuClientState.MenuList;
            this.CurrentLink = MenuClientState.CurrentLink;
            this.MenuStyle = MenuClientState.MenuStyle;
            this.IconPosetion = MenuClientState.IconPostion;
        }

        protected override string SaveClientState()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            //serializer.RegisterConverters(new MenuItemConverter[] { new MenuItemConverter() });
            return serializer.Serialize(new MenuClientState(this.MenuStyle,this.IconPosetion,this.CurrentLink,this.MenuList));

        }

        #region Helper

        [DebuggerStepThrough]
        internal V GetPropertyValue<V>(string propertyName, V nullValue)
        {
            if (ViewState[propertyName] == null)
            {
                return nullValue;
            }
            return (V)ViewState[propertyName];
        }

        [DebuggerStepThrough]
        internal void SetPropertyValue<V>(string propertyName, V value)
        {
            ViewState[propertyName] = value;
        }

        #endregion
    }
    internal class MenuClientState
    {
        public MenuClientState(MenuStyle menuStyle,IconPosetionEnum iconPostion,string currentLink, List<MenuItem> menuList) 
        {
            this.MenuStyle = menuStyle;
            this.IconPostion = iconPostion;
            this.CurrentLink = currentLink;
            this.MenuList = menuList;
        }

        public MenuStyle MenuStyle
        {
            get;
            set;
        }

        public IconPosetionEnum IconPostion
        {
            get;
            set;
        }

        private string _CurrentLink = "";
        public string CurrentLink
        {
            set { _CurrentLink = value; }
            get { return _CurrentLink; }
        }
        //public string Current
        public List<MenuItem> MenuList
        {
            get;
            set;
        } 
    }

    /// <summary>
    /// 菜单项图标的位置
    /// </summary>
    public enum IconPosetionEnum
    {   
        Top=0,
        Left=1
    }

    /// <summary>
    /// 菜单的类型
    /// </summary>
    public enum MenuType
    {
        /// <summary>
        /// 菜单在上
        /// </summary>
        Top=0,
        /// <summary>
        /// 菜单在左
        /// </summary>
        Left=1,
        /// <summary>
        /// 菜单在右
        /// </summary>
        Right=2
    }

    /// <summary>
    /// 菜单样式
    /// </summary>
    public enum MenuStyle
    {
        /// <summary>
        /// 2+级菜单弹出
        /// </summary>
        Popup=0,
        /// <summary>
        /// 2+级菜单内联
        /// </summary>
        Inner=1
    }
}
